package service

import (
	"gorm.io/gorm"
)

type NewColumnService struct {
	db *gorm.DB
}

type NewColumn struct {
	Name     string
	ID       string
	Describe string
	Remark   string
}

func (s *NewColumnService) GetColumns(tableName string) []NewColumn {
	var rs []NewColumn
	rowSql := `
SELECT A.attname AS Name,
	A.attname AS ID,
	d.description AS Describe,
	G.column_remark as "Remark"
FROM
	pg_class
	C INNER JOIN pg_attribute A ON A.attrelid = C.oid 
	AND A.attnum > 0
	INNER JOIN pg_type T ON A.atttypid = T.oid
	INNER JOIN pg_description d ON d.objoid = A.attrelid 
	AND d.objsubid = A.attnum
	LEFT JOIN gen_table_column_config G ON G.TABLE_NAME = C.relname
	AND G.COLUMN_NAME = A.attname 
WHERE
	C.relname = ?
`
	s.db.Raw(rowSql, tableName).Scan(&rs)
	return rs
}
